Attribute VB_Name = "modAllFilesInFolders"
Option Explicit


Public Function FileList(ByVal sFolderPath As String, Optional bWithSubFolders As Boolean = False) As String()
    Dim sTemp As String
    Dim sDirIn As String
    ReDim sFilelist(0) As String, sSubFolderList(0) As String, sToProcessFolderList(0) As String
    Dim i As Integer, j As Integer
    
    sDirIn = sFolderPath
    If Not (Right$(sDirIn, 1) = "\") Then sDirIn = sDirIn & "\"
    
    On Error Resume Next
    sTemp = Dir$(sDirIn & "*.*")
    
    'Loop on Files that aren't Folders
    Do While sTemp <> ""
      'Add file to the list to return
      AddItem2Array1D sFilelist(), sDirIn & sTemp
      sTemp = Dir
    Loop
    
    'Then, if bWithSubFolders is TRUE
    'Process subfolders after common files
    If bWithSubFolders Then
      
      'Loop on Files that aren't Folders
      sTemp = Dir$(sDirIn & "*.*", vbDirectory)
      Do While sTemp <> ""
         ' Ignore current directory and his father
         If sTemp <> "." And sTemp <> ".." Then
            
            ' check if really is a directory
            If (GetAttr(sDirIn & sTemp) And vbDirectory) = vbDirectory Then
              'Add to temporal array to process later, to avoid problems in recurse DIR
              AddItem2Array1D sToProcessFolderList, sDirIn & sTemp
            End If
         End If
         sTemp = Dir   ' Next entry
      Loop
      
      'Process temporal array containing subfolders of current folder
      If UBound(sToProcessFolderList) > 0 Or UBound(sToProcessFolderList) = 0 And sToProcessFolderList(0) <> "" Then
        For i = 0 To UBound(sToProcessFolderList)
          sSubFolderList = FileList(sToProcessFolderList(i), bWithSubFolders)
          If UBound(sSubFolderList) > 0 Or UBound(sSubFolderList) = 0 And sSubFolderList(0) <> "" Then
            For j = 0 To UBound(sSubFolderList)
              AddItem2Array1D sFilelist(), sSubFolderList(j)
            Next
          End If
        Next
      End If

    End If
        
    FileList = sFilelist

End Function



Public Sub AddItem2Array1D(ByRef VarArray As Variant, ByVal VarValue As Variant)
  Dim i  As Long
  Dim iVarType As Integer
  iVarType = VarType(VarArray) - 8192
  i = UBound(VarArray)
  
  Select Case iVarType
      Case vbInteger, vbLong, vbSingle, vbDouble, vbCurrency, vbDecimal, vbByte
      'May fail if First value of VarArray is set to 0
      If VarArray(0) = 0 Then
        i = 0
      Else
        i = i + 1
      End If
            Case VBDate
      'May fail if First value of VarArray is set to #00:00:00#, >:-(
      If VarArray(0) = "00:00:00" Then
        i = 0
      Else
        i = i + 1
      End If
            Case vbString
      'May fail if First value of VarArray is set to ""
      If VarArray(0) = vbNullString Then
        i = 0
      Else
        i = i + 1
      End If
      Case vbBoolean
      'May fail if First value of VarArray is set to False
      If VarArray(0) = False Then
        i = 0
      Else
        i = i + 1
      End If
          End Select
    ReDim Preserve VarArray(i)
  VarArray(i) = VarValue
  End Sub


Public Function FolderList(SPath As String) As String()
Dim fName As String, iCount As Integer, folders() As String
iCount = 0

fName = Dir(SPath, vbDirectory)


Do While fName <> ""
If fName <> "." And fName <> ".." Then
If (GetAttr(SPath & fName) And vbDirectory) = vbDirectory Then
If Not ArrayInitialised(ArrPtr(folders)) Then
ReDim folders(0) As String
Else
ReDim folders(0 To UBound(folders) + 1)
End If
folders(UBound(folders)) = fName
iCount = iCount + 1
MsgBox fName
End If
End If
fName = Dir
Loop
FolderList = folders
End Function
